package zl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.bean.Category;
import zl.bean.Novel;
import zl.mapper.NovelMapper;
import zl.service.NovelService;

import java.sql.SQLException;
import java.util.List;

@Service
public class NovelServiceImpl implements NovelService {
    @Autowired
    private NovelMapper novelMapper;
    @Override
    public int addNovel(Novel novel) {
        return novelMapper.addNovel(novel);
    }

    @Override
    public int deleteNovel(Integer id) {
        return novelMapper.deleteNovel(id);
    }

    @Override
    public int updateNovel(Novel novel) {
        return novelMapper.updateNovel(novel);
    }

    @Override
    public PageInfo<Novel> selectByPage(int currentPage ,String keyword) throws SQLException {
        PageHelper.startPage(currentPage, 3);
        List<Novel> list =novelMapper.selectAllNovel(keyword);
        PageInfo<Novel> pageInfo = new PageInfo<>(list);
        System.out.println("总记录数：" + pageInfo.getTotal());
        System.out.println("当前页：" + pageInfo.getPageNum());
        System.out.println("总页数：" + pageInfo.getPages());
        System.out.println("上一页：" + pageInfo.getPrePage());
        System.out.println("下一页：" + pageInfo.getNextPage());
        System.out.println("是否是首页：" + pageInfo.isIsFirstPage());
        System.out.println("是否是尾页：" + pageInfo.isIsLastPage());
        return pageInfo;
    }

    @Override
    public List<Category> selectAllCategory() {
        return novelMapper.selectAllCategory(null);
    }

    @Override
    public Novel selectById(Integer id) {

        return novelMapper.selectById(id);
    }

    @Override
    public List<Novel> selectAllNovel() {
        return novelMapper.selectAllNovel(null);
    }
}
